package com.android.picotts.businiss;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.android.picotts.businiss.model.Spriter;
import com.android.picotts.util.Constant;
import com.android.picotts.util.MessageBus;
import com.android.picotts.util.MessageBus.MMessage;

/** 精灵管理器*/
public class SpriterManager {
    
    public static final String DATA_INFO = "datainfo";
    private List<Spriter> spritersList = new ArrayList<Spriter>();
    private Context mContext;
    
    private SpriterManager(Context context) {
        mContext = context;
    }
    private static SpriterManager instance;
    public static SpriterManager getInstance(Context context) {
        if(instance == null) {
            instance = new SpriterManager(context);
        }
        return instance;
    }
    
    public synchronized void add(Spriter one) {
        for(Spriter spirte : spritersList) {
            if(spirte.equals(one)) {
                // 已存在
                return;
            }
        }
        spritersList.add(one);
        MMessage msg = MessageBus.getBusFactory().createMessage( Constant.MSG_ADD_SPRITE);
        msg.obj = one;
        MessageBus.getBusFactory().send(msg);
        
        save();
    }
    
    public synchronized void remove(Spriter one) {
        spritersList.remove(one);
        MMessage msg = MessageBus.getBusFactory().createMessage( Constant.MSG_DEL_SPRITE);
        msg.obj = one;
        MessageBus.getBusFactory().send(msg);
        
        save();
    }
    
    public List<Spriter> getList(){
        return spritersList;
    }
    
    public void save() {        
        FileOutputStream fos = null;
        try {
            fos = mContext.openFileOutput(DATA_INFO,Context.MODE_PRIVATE);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(fos);
            if(spritersList == null) {
                spritersList = new ArrayList<Spriter>(); 
            }
            oos.writeObject(spritersList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    public void load() {
        FileInputStream fis = null;
        try {
            fis = mContext.openFileInput(DATA_INFO);            
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(fis);
            spritersList = (List<Spriter>)ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
}